// pages/cash/result.js
const {
  $Message
} = require("../../dist/base/index");
import api from "../../utils/apis.js";
import message from "../../utils/message.js";
import util from "../../utils/util.js";

Page({
  /**
   * Page initial data
   */
  data: {
    coinName: "",
    value: "",
    visible: false,
    balance: null,
    walletAddress: null
  },

  sub() {
    let self = this;
    if (!self.data.value) {
      $Message({
        content: "请输入您要提现的数目",
        type: "warning"
      });
      return;
    }
    if (parseInt(self.data.value) < 100) {
      $Message({
        content: "提现数不能少于100",
        type: "warning"
      });
      return;
    }
    this.setData({
      visible: true
    });
  },
  bindKeyInput: function(e) {
    let self = this;
    self.setData({
      value: e.detail.value
    });
  },
  sure() {
    let self = this;
    self.getUserProfile(self.data.coinName);
    self.setData({
      visible: false
    });
  },

  cancel() {
    this.setData({
      visible: false
    });
    $Message({
      content: "您已取消提现申请",
      type: "warning"
    });
  },

  getUserProfile(coinType) {
    let requestCoinType = null;
    if (coinType == "STORJ") {
      requestCoinType = "1";
    } else if (coinType == "FIL") {
      requestCoinType = "4";
    } else if (coinType == "UT") {
      requestCoinType = "3";
    } else if (coinType == "PLC") {
      requestCoinType = "2";
    }
    let self = this;
    let param = {
      cashAmount: self.data.value,
      coinType: requestCoinType
    };

    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });

    wx.request({
      url: api.cash + requestCoinType + "?" + "cashAmount=" + self.data.value,
      method: "post",
      data: param,
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        console.log(res);
        if (res.statusCode == "200") {
          $Message({
            content: "您已成功提交" + self.data.value + "个" + self.data.coinName,
            type: "success"
          });
          wx.navigateTo({
            url: "/pages/wallet/wallet?coinName=" +
              self.data.coinName +
              "&balance=" +
              self.data.balance +
              "&walletAddress=" +
              self.data.walletAddress
          });
        } else {
          message.showInfo(
            res.data.message ? res.data.message : "网络连接异常"
          );
        }
      }
    });
  },
  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
    this.setData({
      coinName: options.coinName,
      balance: options.balance,
      walletAddress: options.walletAddress
    });

    let self = this;

    wx.checkSession({
      success() {
        let v = wx.getStorageSync("TOKEN");
        if (v) {
          console.log(v);
        } else {
          self.wxlogin();
        }
        //session_key 未过期，并且在本生命周期一直有效
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        self.wxlogin();
      }
    });
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {},

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {},

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {},

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {},

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {},

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {},

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {},
  //微信授权相关
  getUserInfo: function(e) {
    console.log(e);
    app.globalData.userInfo = e.detail.userInfo;
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    });
  },
  loginAction() {
    let self = this;

    let param = {
      code: self.data.authCode,
      grantType: "wechat_mini_app",
      login: "",
      rememberMe: true
    };

    wx.showLoading({
      title: "加载中"
    });

    wx.request({
      url: api.loginApi,
      method: "POST",
      data: param,
      header: {
        "content-type": "application/json"
      },
      success(res) {
        wx.hideLoading();
        if (res.statusCode == "200") {
          let token = res.data.id_token;

          try {
            wx.setStorageSync("authorization", token);
          } catch (e) {
            console.log(e);
          }
        } else {
          message.showInfo(
            res.data.message ? res.data.message : "网络连接异常"
          );
        }
      }
    });
  },
  wxlogin() {
    let self = this;

    wx.login({
      success(res) {
        console.log(res);
        if (res.code) {
          if (res.errMsg == "login:ok") {
            self.setData({
              authCode: res.code
            });
            self.loginAction();
          }
        } else {
          console.log("登录失败！" + res.errMsg);
        }
      }
    });
  }
});